package com.meefo.meeting.web.front;

import com.meefo.meeting.common.QRCodeGenerate;
import com.meefo.meeting.config.Constants;
import com.meefo.meeting.persistence.bean.MeetingFile;
import com.meefo.meeting.persistence.bean.MeetingInfo;
import com.meefo.meeting.persistence.bean.SignInfo;
import com.meefo.meeting.persistence.service.MeetingService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/front")
public class meetController {
    @Autowired
    MeetingService meetingService;

    @ResponseBody
    @RequestMapping("saveMeeting")
    //创建会议
    public JSONObject saveMeeting(String appKey,String requestKey,String subject,String content,String place,
                                  String start_date,String meeting_period,String mtType,Integer attendance_people,
                                  Float meeting_money,String is_net_meeting,String apply_unit,String apply_unit_name,
                                  String apply_user,String apply_user_name){
        JSONObject object = new JSONObject();
        Boolean result= null;
        String code="200";
        String msg="创建会议成功";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                result = meetingService.saveMeeting(subject,content,place,
                        start_date,meeting_period,mtType,attendance_people,meeting_money,
                        is_net_meeting,apply_unit,apply_unit_name, apply_user,apply_user_name);
                if(false==result){
                    code="-201";
                    msg="请求失败";
                }
            } catch (Exception e) {
                e.printStackTrace();
                code="201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        object.put("code",code);
        object.put("msg",msg);
        return object;
    }

    @ResponseBody
    @RequestMapping("queryMeetList")
    //查找会议
    public JSONObject queryMeetList(String appKey,String requestKey,String apply_unit, String subject, String start_date, String end_date){
        JSONObject object = new JSONObject();
        String code="200";
        String msg="会议查询成功";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                List<MeetingInfo> meetingInfos= meetingService.queryMeetList(apply_unit,subject,start_date,end_date);
                if(StringUtils.isEmpty(meetingInfos)){
                    code="-202";
                    msg="输入参数错误";
                    object.put("code",code);
                    object.put("msg",msg);
                }else{
                    object.put("code",code);
                    object.put("msg",msg);
                    object.put("data",meetingInfos);
                }
            } catch (Exception e) {
                e.printStackTrace();
                code="-201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        return object;
    }

    @ResponseBody
    @RequestMapping("queryMeetInfo")
    //会议详情
    public JSONObject queryMeetInfo(String appKey,String requestKey,String meeting_id){
        JSONObject object = new JSONObject();
        String code="200";
        String msg="会议查询成功";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                Map<String,Object> data= meetingService.queryMeetInfo(meeting_id);
                if(StringUtils.isEmpty(data)){
                    code="-202";
                    msg="输入参数错误";
                    object.put("code",code);
                    object.put("msg",msg);
                }else{
                    object.put("code",code);
                    object.put("msg",msg);
                    object.put("data",data);
                }
            } catch (Exception e) {
                e.printStackTrace();
                code="-201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        return object;
    }

    @ResponseBody
    @RequestMapping("deleteInfo")
    //删除会议
    public JSONObject deleteInfo(String appKey,String requestKey,String meeting_id,String delete_user){
        JSONObject object = new JSONObject();
        String code="200";
        String msg="删除会议成功";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                Boolean flag= meetingService.deleteInfo(meeting_id,delete_user);
                if(!flag){
                    code="-202";
                    msg="输入参数错误";
                }
                object.put("code",code);
                object.put("msg",msg);
            } catch (Exception e) {
                e.printStackTrace();
                code="-201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        return object;
    }

    /**
     *
     * @param request
     * @param meeting_id
     * @param callback_url 加密后的url路径
     * @return
     */
    @ResponseBody
    @RequestMapping("generateQRCode")
    //生成二维码
    public JSONObject generateQRCode(String appKey,String requestKey,HttpServletRequest request,String meeting_id,String callback_url){
        JSONObject jsonObject = new JSONObject();
        String host = "";
        String adress = "";
        String code= null;
        String msg= null;
        String result=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(result)){
            code="-203";
            msg="接口签名错误";
            jsonObject.put("code",code);
            jsonObject.put("msg",msg);
        }else if(("overDue").equals(result)){
            code="-205";
            msg="签名过期";
            jsonObject.put("code",code);
            jsonObject.put("msg",msg);
        }else{
            try {
                File file = null;
                host = InetAddress.getLocalHost().getHostAddress();
                //局域网内ip地址
                adress = Constants.proUri + "/front/scanCode?meetingid=" + meeting_id+"&callback_url="+callback_url;
//                    "&sign_user"+userId+"&sign_user_name="+sign_user_name;
                MeetingInfo meetingInfo = meetingService.getMeetingById(meeting_id);
                JSONObject meetingQRCode = QRCodeGenerate.getMeetingQRCode(meetingInfo.getSubject(), adress);
                String path = meetingQRCode.getString("path");
                file = new File(path);
                if (meetingQRCode.getBoolean("result")) {
                    meetingInfo.setQrCode(meetingQRCode.getString("httppath"));
                    meetingInfo.setQrCallbackUrl(callback_url);
                    meetingService.updateQrCode(meetingInfo);
                    jsonObject.put("code", 200);
                    jsonObject.put("url", meetingInfo.getQrCode());
                    System.out.println(meetingInfo.getQrCode());
                    jsonObject.put("name", meetingInfo.getSubject());
                } else {
                    jsonObject.put("code", 500);
                }
            } catch (IOException e) {
                e.printStackTrace();
                e.printStackTrace();
                code="-201";
                msg="接口exception";
                jsonObject.put("code",code);
                jsonObject.put("msg",msg);
                return jsonObject;
            }
        }
        return jsonObject;
    }

    /**
     *  扫码签到模块
     * @param meeting_id 会议ID
     * @param callback_url 用于请求人员接口
     * @return
     */
    @ResponseBody
    @RequestMapping("scanCode")
    public JSONObject scanCode(String appKey,String requestKey,String meeting_id,String callback_url){

        JSONObject object = new JSONObject();
        String code= null;
        String msg= null;
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                JSONObject jsonobject=meetingService.getUserJsonFormUrl(callback_url);
                String sign_user=jsonobject.get("sign_user").toString();
                String sign_user_name=jsonobject.get("sign_user_name").toString();
                Boolean result=meetingService.scanCode(meeting_id,sign_user,sign_user_name);
                code = "200";
                msg = "扫码成功";
                if(!result){
                    code="-202";
                    msg="输入参数错误";
                }
            } catch (Exception e) {
                e.printStackTrace();
                code="-201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        object.put("code",code);
        object.put("msg",msg);
        return object;
    }

    /**
     * 上传会议后文件，上传完成后关闭会议
     * @param meeting_id
     * @param Files
     * @param create_user
     * @return
     */
    @ResponseBody
    @RequestMapping("meetEnd")
    public JSONObject scanCode(String appKey,String requestKey,String meeting_id, MultipartFile Files[], String create_user){
        JSONObject object = new JSONObject();
        String code="-200";
        String msg="文件上传成功，会议已完结";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                Boolean result=meetingService.FileUpload(meeting_id,Files,"2",create_user);
                if(!result){
                    code="-202";
                    msg="输入参数错误";
                }
                object.put("code",code);
                object.put("msg",msg);
            } catch (Exception e) {
                e.printStackTrace();
                code="-201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        return object;
    }

    /**
     * 上传会议前文件
     * @param meeting_id
     * @param Files
     * @param create_user
     * @return
     */
    @ResponseBody
    @RequestMapping("fileUpload")
    public JSONObject fileUpload(String appKey,String requestKey,String meeting_id, MultipartFile Files[], String create_user){
        JSONObject object = new JSONObject();
        String code="200";
        String msg="文件上传成功";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                Boolean result=meetingService.FileUpload(meeting_id,Files,"1",create_user);
                if(!result){
                    code="-202";
                    msg="输入参数错误";
                }
                object.put("code",code);
                object.put("msg",msg);
            } catch (Exception e) {
                e.printStackTrace();
                code="-201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        return object;
    }
    /**
     * 根据会议id查询参会人员
     * @param meeting_id
     * @return
     */
    @ResponseBody
    @RequestMapping("queryPersons")
    public JSONObject queryPersons(String appKey,String requestKey,String meeting_id){
        JSONObject object = new JSONObject();
        String code="200";
        String msg="查询成功";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                List<SignInfo> signInfoList=meetingService.queryAllSignedPersons(meeting_id);
                object.put("code",code);
                object.put("msg",msg);
                object.put("data",signInfoList);
            } catch (Exception e) {
                e.printStackTrace();
                code="-201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        return object;
    }

    /**
     * 查询文件列表的文件
     * @param meeting_id 会议id
     * @param type 文件类型
     * @param file_name 文件名称
     * @return
     */
    @ResponseBody
    @RequestMapping("queryFiles")
    public JSONObject queryFiles(String appKey,String requestKey,String meeting_id,String type,String file_name){
        JSONObject object = new JSONObject();
        String code="200";
        String msg="查询成功";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                List<MeetingFile> fileList=meetingService.queryMeetingFileByMeetingIdOrFileNameOrType(meeting_id,type,file_name);
                object.put("code",code);
                object.put("msg",msg);
                object.put("data",fileList);
            } catch (Exception e) {
                code="-201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        return object;
    }

    /**
     * 删除指定的文件
     * @param meeting_id 会议ID
     * @param create_user 创建人
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteFile")
    public JSONObject deleteFile(String appKey,String requestKey,String meeting_id,String create_user){
        JSONObject object = new JSONObject();
        String code="200";
        String msg="删除成功";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            try {
                String result=meetingService.deleteFile(meeting_id,create_user);
                if(("success").equals(result)){
                    object.put("code",code);
                    object.put("msg",msg);
                }else if(("false").equals(result)){
                    code="-202";
                    msg="输入参数错误";
                    object.put("code",code);
                    object.put("msg",msg);
                }else{

                }
            } catch (Exception e) {
                code="-201";
                msg="接口exception";
                object.put("code",code);
                object.put("msg",msg);
                return object;
            }
        }
        return object;
    }

    /**
     * 申请方初次获取 requestKey
     * @param appKey
     * @param secretKey
     * @param requestTime unix时间戳
     * @return
     */
    @ResponseBody
    @RequestMapping("getSecretKey")
    public JSONObject getSecretKey(String appKey,String secretKey,String requestTime){
        JSONObject object = new JSONObject();
        String code="200";
        String msg="操作成功";
        String request=meetingService.getSecretKey(appKey,secretKey,requestTime);
        if(("false").equals(request)){
            code="-202";
            msg="接口输入参数错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("error").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            object.put("code",code);
            object.put("msg",msg);
            object.put("data",request);
        }
        return object;
    }

    /**
     * 验证缓存中的签名数据是否存在
     * @param appKey
     * @param requestKey
     * @return
     */
    @ResponseBody
    @RequestMapping("verifyKeyIsTrue")
    public JSONObject verifyKeyIsTrue(String appKey,String requestKey){
        JSONObject object = new JSONObject();
        String code="200";
        String msg="验证成功";
        String request=meetingService.verifyKeyIsTrue(appKey,requestKey);
        if(("false").equals(request)){
            code="-203";
            msg="接口签名错误";
            object.put("code",code);
            object.put("msg",msg);
        }else if(("overDue").equals(request)){
            code="-205";
            msg="签名过期";
            object.put("code",code);
            object.put("msg",msg);
        }else{
            object.put("code",code);
            object.put("msg",msg);
            object.put("data",request);
        }
        return object;
    }
}
